﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using UmcProvider.Controls;
using System.Windows.Forms;
using System.Reflection;
using UmcProvider;

namespace ChinaTelecom.SmartPhone.Controls
{
    //[Designer(typeof(UmcContainerDesigner<ITabControl>))]
    [ToolboxBitmap(typeof(System.Windows.Forms.TabControl), "TabControl.bmp")]
    public sealed class TabControl : System.Windows.Forms.TabControl, ITabControl
    {
    }

    public interface ITabControl : IControl { }
    public sealed class UmcTabControl : DefaultConverter<UmcTabControl, TabControl>, ITabControl
    {
        public override XmlNode[] Serialize(System.ComponentModel.IComponent root, XmlDocument document)
        {
            XmlNode[] nodes = base.Serialize(root, document);

            if (nodes.Length > 0)
            {
                XmlNode tabControlNode = nodes[0];
                TabControl tabControl = root as TabControl;
                if (tabControl != null)
                {
                    TabControl.TabPageCollection tabPages = tabControl.TabPages;
                    foreach (TabPage tabPage in tabPages)
                    {
                        XmlNode tabPageNode = document.CreateNode(XmlNodeType.Element, "TabPage", null);
                        tabPageNode.Attributes.Append(document.CreateAttribute("Text"))
                            .Value = tabPage.Text;
                        tabControlNode.AppendChild(tabPageNode);

                        //                      
                        foreach (IComponent child in tabPage.Controls)
                        {
                            IConverter ict = UmcSerializeFactory.Create(child);
                            if (ict != null)
                            {
                                XmlNode[] xn = ict.Serialize(child, document);
                                tabPageNode.AppendChild(xn.First());
                            }
                        }
                    }
                }
            }

            return nodes;
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            IComponent component = base.DeSerialize(host, document, node);
            TabControl tabControl = component as TabControl;

            if (node.Length > 0 && tabControl != null)
            {
                XmlNode tabControlNode = node[0];
                foreach (XmlNode tabPageNode in tabControlNode.ChildNodes)
                {
                    if (tabPageNode.NodeType == XmlNodeType.Element && tabPageNode.Name == "TabPage")
                    {
                        TabPage tabPage = new TabPage();
                        if (!string.IsNullOrEmpty(tabPageNode.Attributes["Text"].Value))
                        {
                            PropertyInfo prop = typeof(TabPage).GetProperty("Text");
                            prop.SetValue(tabPage, tabPageNode.Attributes["Text"].Value, null);
                        }

                        tabPage.BackColor = Color.White;
                        tabControl.TabPages.Add(tabPage);

                        foreach (XmlNode controlInTabPageNode in tabPageNode.ChildNodes)
                        {
                            IConverter ict = UmcDeSerializeFactory.Create(controlInTabPageNode.Name);
                            IComponent componentInTabPage = ict.DeSerialize(host, document, new XmlNode[] { controlInTabPageNode });
                            Control controlInTabPage = componentInTabPage as Control;
                            if (controlInTabPage != null)
                            {
                                tabPage.Controls.Add(controlInTabPage);
                            }
                        }
                    }
                }
            }

            return component;
        }

        #region Hidden
        public bool Enabled
        {
            get { throw new NotImplementedException(); }
        }

        public bool Visible
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Size Size
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Point Location
        {
            get { throw new NotImplementedException(); }
        }

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Color BackColor
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Font Font
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Color ForeColor
        {
            get { throw new NotImplementedException(); }
        }

        public string Name
        {
            get { throw new NotImplementedException(); }
        }
        #endregion
    }
}
